<template>
    <div class="edit-popup">
        <popup ref="popupRef" :title="popupTitle" :async="true" width="550px" @confirm="handleSubmit"
            @close="handleClose">
            <el-form ref="formRef" :model="formData" label-width="90px" :rules="formRules">
                <el-form-item label="版本" prop="type">
                    <el-select class="flex-1" v-model="formData.type" clearable placeholder="请选择版本">
                        <el-option label="基础版" value="jichu" />
                        <el-option label="高级版" value="gaoji" />
                    </el-select>
                </el-form-item>
                <el-form-item label="型号" prop="spec">
                    <el-input v-model="formData.spec" clearable placeholder="请输入型号" />
                </el-form-item>
                <el-form-item label="内存" prop="disk">
                    <el-input v-model="formData.disk" clearable placeholder="请输入内存" />
                </el-form-item>
                <el-form-item label="屏幕状态" prop="screen">
                    <el-select class="flex-1" v-model="formData.screen" clearable placeholder="请选择版本">
                        <el-option label="划痕明显" value="dahua" />
                        <el-option label="轻微划痕" value="xiaohua" />
                    </el-select>
                </el-form-item>
                <el-form-item label="收购价" prop="old_price">
                    <el-input v-model="formData.old_price" clearable placeholder="请输入" />
                </el-form-item>

                <el-form-item label="换新机型" prop="target_spec">
                    <el-input v-model="formData.target_spec" clearable placeholder="请输入换新机型" />
                </el-form-item>
                <el-form-item label="换新内存" prop="target_disk">
                    <el-input v-model="formData.target_disk" clearable placeholder="请输入换新内存" />
                </el-form-item>
                <el-form-item label="新购价" prop="renew_price">
                    <el-input v-model="formData.renew_price" clearable placeholder="请输入" />
                </el-form-item>
                <el-form-item label="保费" prop="baofei">
                    <el-input v-model="formData.baofei" clearable placeholder="请输入" />
                </el-form-item>
            </el-form>
        </popup>
    </div>
</template>

<script lang="ts" setup name="renewEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiRenewAdd, apiRenewEdit, apiRenewDetail } from '@/api/renew'
import { timeFormat } from '@/utils/util'
import type { PropType } from 'vue'
defineProps({
    dictData: {
        type: Object as PropType<Record<string, any[]>>,
        default: () => ({})
    }
})
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')


// 弹窗标题
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑换新差价' : '新增换新差价'
})

// 表单数据
const formData = reactive({
    id: '',
    type: '',
    spec: '',
    disk: '',
    screen: '',
    target_spec: '',
    target_disk: '',
    price: '',

    old_price: '',
    renew_price: '',
    baofei: ''
})


// 表单验证
const formRules = reactive<any>({
    type: [{
        required: true,
        message: '请选择版本',
        trigger: ['blur']
    }],
    spec: [{
        required: true,
        message: '请输入型号',
        trigger: ['blur']
    }],
    disk: [{
        required: true,
        message: '请输入内存',
        trigger: ['blur']
    }],
    screen: [{
        required: true,
        message: '请输入屏幕状态',
        trigger: ['blur']
    }],
    target_spec: [{
        required: true,
        message: '请输入换新机型',
        trigger: ['blur']
    }],
    target_disk: [{
        required: true,
        message: '请输入换新内存',
        trigger: ['blur']
    }],
    price: [{
        required: true,
        message: '请输入',
        trigger: ['blur']
    }],
    old_price: [{
        required: true,
        message: '请输入',
        trigger: ['blur']
    }],
    renew_price: [{
        required: true,
        message: '请输入',
        trigger: ['blur']
    }]
})


// 获取详情
const setFormData = async (data: Record<any, any>) => {
    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            //@ts-ignore
            formData[key] = data[key]
        }
    }


}

const getDetail = async (row: Record<string, any>) => {
    const data = await apiRenewDetail({
        id: row.id
    })
    setFormData(data)
}


// 提交按钮
const handleSubmit = async () => {
    await formRef.value?.validate()
    const data = { ...formData, }
    mode.value == 'edit'
        ? await apiRenewEdit(data)
        : await apiRenewAdd(data)
    popupRef.value?.close()
    emit('success')
}

//打开弹窗
const open = (type = 'add') => {
    mode.value = type
    popupRef.value?.open()
}

// 关闭回调
const handleClose = () => {
    emit('close')
}



defineExpose({
    open,
    setFormData,
    getDetail
})
</script>
